
import 'dart:convert';
import 'package:ichinesereader/utils/enumValues.dart';

UserAvatar userAvatarFromJson(String str) =>
    UserAvatar.fromJson(json.decode(str));

String userAvatarToJson(UserAvatar data) => json.encode(data.toJson());

class UserAvatar {
  UserAvatar({
    this.head,
    this.body,
    this.legs,
    this.arms,
    this.accessory,
  });

  List<Accessory>? head;
  List<Accessory>? body;
  List<Accessory>? legs;
  List<Accessory>? arms;
  List<Accessory>? accessory;

  factory UserAvatar.fromJson(Map<String, dynamic> json) => UserAvatar(
        head: List<Accessory>.from(
            json["head"].map((x) => Accessory.fromJson(x))),
        body: List<Accessory>.from(
            json["body"].map((x) => Accessory.fromJson(x))),
        legs: List<Accessory>.from(
            json["legs"].map((x) => Accessory.fromJson(x))),
        arms: List<Accessory>.from(
            json["arms"].map((x) => Accessory.fromJson(x))),
        accessory: List<Accessory>.from(
            json["accessory"].map((x) => Accessory.fromJson(x))),
      );

  Map<String, dynamic> toJson() => {
        "head": List<dynamic>.from(head!.map((x) => x.toJson())),
        "body": List<dynamic>.from(body!.map((x) => x.toJson())),
        "legs": List<dynamic>.from(legs!.map((x) => x.toJson())),
        "arms": List<dynamic>.from(arms!.map((x) => x.toJson())),
        "accessory": List<dynamic>.from(accessory!.map((x) => x.toJson())),
      };
}

class Accessory {
  Accessory({
    this.id,
    this.userId,
    this.itemId,
    this.shopType,
    this.subType,
    this.isEquipped,
    this.v,
    this.details,
  });

  String? id;
  String? userId;
  String? itemId;
  ShopType? shopType;
  String? subType;
  bool? isEquipped;
  int? v;
  Details? details;

  factory Accessory.fromJson(Map<String, dynamic> json) => Accessory(
        id: json["_id"],
        userId: json["user_id"],
        itemId: json["item_id"],
        shopType: shopTypeValues.map[json["shop_type"]],
        subType: json["sub_type"],
        isEquipped: json["is_equipped"],
        v: json["__v"],
        details: Details.fromJson(json["details"]),
      );

  Map<String, dynamic> toJson() => {
        "_id": id,
        "user_id": userId,
        "item_id": itemId,
        "shop_type": shopTypeValues.reverse[shopType],
        "sub_type": subType,
        "is_equipped": isEquipped,
        "__v": v,
        "details": details?.toJson(),
      };
}

class Details {
  Details({
    this.name,
    this.cost,
    this.type,
    this.subType,
    this.v,
    this.asset,
    this.shopType,
    this.resolutions,
  });

  String? name;
  int? cost;
  String? type;
  SubType? subType;
  int? v;
  String? asset;
  ShopType? shopType;
  Resolutions? resolutions;

  factory Details.fromJson(Map<String, dynamic> json) => Details(
        name: json["name"],
        cost: json["cost"],
        type: json["type"],
        subType: subTypeValues.map[json["sub_type"]],
        v: json["_v"],
        asset: json["asset"],
        shopType: shopTypeValues.map[json["shop_type"]],
        resolutions: Resolutions.fromJson(json["resolutions"]),
      );

  Map<String, dynamic> toJson() => {
        "name": name,
        "cost": cost,
        "type": type,
        "sub_type": subTypeValues.reverse[subType],
        "_v": v,
        "asset": asset,
        "shop_type": shopTypeValues.reverse[shopType],
        "resolutions": resolutions?.toJson(),
      };
}

class Resolutions {
  Resolutions({
    this.the1X,
    this.the2X,
    this.hdpi,
    this.web,
    this.xhdpi,
    this.xxhdpi,
  });

  String? the1X;
  String? the2X;
  String? hdpi;
  String? web;
  String? xhdpi;
  String? xxhdpi;

  factory Resolutions.fromJson(Map<String, dynamic> json) => Resolutions(
        the1X: json["@1x"],
        the2X: json["@2x"],
        hdpi: json["hdpi"],
        web: json["web"],
        xhdpi: json["xhdpi"],
        xxhdpi: json["xxhdpi"],
      );

  Map<String, dynamic> toJson() => {
        "@1x": the1X,
        "@2x": the2X,
        "hdpi": hdpi,
        "web": web,
        "xhdpi": xhdpi,
        "xxhdpi": xxhdpi,
      };
}

enum ShopType { AVATAR }

final shopTypeValues = EnumValues({"AVATAR": ShopType.AVATAR});

enum SubType { HAT_HAIR, EMPTY }

final subTypeValues =
    EnumValues({"": SubType.EMPTY, "hat_hair": SubType.HAT_HAIR});